// 
// $Rev:: 25            $:  Revision of last commit
// $Author:: rvhaverm@g#$:  Author of last commit
// $Date:: 2010-07-26 1#$:  Date of last commit
// 
package agents.listeners;

import com.ibm.aglet.InvalidAgletException;
import com.ibm.aglet.system.ContextEvent;

import agents.interfaces.IHostEventListener;


public class ConsoleHostEventListener implements IHostEventListener
{

    private void aux(ContextEvent ev, String eventName)
    {
	System.out.print(ev.getAgletContext().getName());
	System.out.print(": ");

	if (ev.getAgletProxy() != null)
	{
	    System.out.print("aglet '");
	    String agletName = "";
	    try
	    {
		agletName = ev.getAgletProxy().getAgletClassName();
	    }
	    catch (InvalidAgletException e)
	    {
		agletName = "INVALID AGLET";
	    }
	    System.out.print(agletName);

	    System.out.print("' ");
	}

	System.out.println(eventName);
    }

    @Override
    public void agletActivated(ContextEvent ev)
    {
	aux(ev, "activated");
    }

    @Override
    public void agletArrived(ContextEvent ev)
    {
	aux(ev, "arrived");
    }

    @Override
    public void agletCloned(ContextEvent ev)
    {
	aux(ev, "cloned");
    }

    @Override
    public void agletCreated(ContextEvent ev)
    {
	aux(ev, "created");
    }

    @Override
    public void agletDeactivated(ContextEvent ev)
    {
	aux(ev, "deactivated");
    }

    @Override
    public void agletDispatched(ContextEvent ev)
    {
	aux(ev, "dispatched");
    }

    @Override
    public void agletDisposed(ContextEvent ev)
    {
	aux(ev, "disposed");
    }

    @Override
    public void agletResumed(ContextEvent ev)
    {
	aux(ev, "resumed");
    }

    @Override
    public void agletReverted(ContextEvent ev)
    {
	aux(ev, "reverted");
    }

    @Override
    public void agletStateChanged(ContextEvent ev)
    {
	aux(ev, "state changed");
    }

    @Override
    public void agletSuspended(ContextEvent ev)
    {
	aux(ev, "suspended");
    }

    @Override
    public void contextShutdown(ContextEvent ev)
    {
	aux(ev, "context shutdown");
    }

    @Override
    public void contextStarted(ContextEvent ev)
    {
	aux(ev, "context started at " + ev.getAgletContext().getHostingURL());
    }

    @Override
    public void showDocument(ContextEvent ev)
    {
	aux(ev, "show document");
    }

    @Override
    public void showMessage(ContextEvent ev)
    {	
	aux(ev, ev.getMessage());
    }

}
